﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;

namespace docmgr.WinForm
{
    public partial class DocConcernedList : DockContent
    {
        /// <summary>
        /// 
        /// </summary>
        public DocConcernedList()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 卡木糖，未曾放弃。前奏听起来还不错。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DocConcernedList_Load(object sender, EventArgs e)
        {
            init();
        }

        void init()
        {
            String sqlstr = String.Format("SELECT  dc.ID,doc.ID DocID,doc.DocName 文件名,u.Name 责任人 ,doc.Portion 份数 ,doc.SecretLevel 密级  from DocConcerned as dc left join Document as doc on dc.DocID=doc.ID left join [User] as u on doc.UserID =u.ID WHERE dc.UserID= {0} order by dc.id desc", Convert.ToInt32(Common.curUser.Rows[0]["ID"]));

            dataGridView1.DataSource = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlstr).Tables[0];
        }
        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex > -1 && e.RowIndex > -1)
            {
                int key =Convert.ToInt32( dataGridView1.Rows[e.RowIndex].Cells["DocID"].Value.ToString());

                String sqlStr = String.Format("SELECT dbo.Document.DocName 文件名, dbo.[User].Name 转发人,drl.StartDate 转发日,u.name 批阅人,drl.ReadDate 批阅日期,  drl.Status 状态,drl.Comments 意见 FROM dbo.DocReadLog AS drl left JOIN dbo.[User] ON drl.LeaderId = dbo.[User].ID left JOIN dbo.Document ON drl.DocumentID = dbo.Document.ID     LEFT JOIN [User] as u on drl.ReadUserID= u.ID  where drl.DocumentID={0} order by drl.id desc", key);

                dataGridView2.DataSource = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];

                sqlStr = String.Format("select count(*) c from DocReadLog as drl where status='已读' and drl.DocumentID={0}", key);
                label1.Text = "批阅次数:0";
                try
                {
                    label1.Text = "批阅次数:" + Convert.ToInt32(SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0].Rows[0]["c"]);
                }
                catch(Exception ee1)
                {
                    MessageBox.Show(ee1.StackTrace);
                }
                sqlStr = String.Format("select count(*) c from( select count(*) c from DocReadLog as drl where status='已读' and drl.DocumentID={0} group by ReadUserID) as cc", key);
                label2.Text = "批阅人数:0";
                try
                {
                    label2.Text = "批阅人数:" + Convert.ToInt32(SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0].Rows[0]["c"]);
                }
                catch(Exception ee2)
                {
                    MessageBox.Show(ee2.StackTrace);
                }
                sqlStr = String.Format("select count(*) c from (select count(*) c from DocReadLog as drl where status='未读' and drl.DocumentID={0} group by ReadUserID) as cc", key);
                label3.Text = "尚未批阅人数:0";
                try
                {
                    label3.Text = "尚未批阅人数:" + Convert.ToInt32(SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0].Rows[0]["c"]);
                }
                catch(Exception ee3)
                {
                    MessageBox.Show(ee3.StackTrace);
                }
                sqlStr = String.Format("SELECT doc.DocName 文件名,dc.Code 编码, u.Name 持有人, dc.Status 状态, dc.SysSerialNumber 卷号  FROM dbo.DocHandler AS dc INNER JOIN dbo.[User] AS u ON dc.HandlerUserID = u.ID INNER JOIN dbo.Document AS doc ON dc.DocID = doc.ID WHERE dc.DocID = {0} ORDER BY dc.ID DESC ", key);

                dataGridView3.DataSource = SQLHelper.ExecuteDataSet(SQLHelper.connString, sqlStr).Tables[0];

            }
            
        }

        private void DocConcernedList_DoubleClick(object sender, EventArgs e)
        {
           
        }

        private void button1_Click(object sender, EventArgs e)
        {
            init();
        }
    }
}
